package com.bang.concurrency.example.threadPool;

import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: ThreadPoolExample1
 * @Desc: ExecutorService executorService = Executors.newSingleThreadExecutor();
 * @auther: BANG
 * @Date: 2019/10/09 14:15
 * @Version: 1.0
 * @Modified By:
 */
@Slf4j
public class ThreadPoolExample5 {

    public static void main(String[] args) throws Exception {

//        test2();
//        test3();
//        test4();
//        test5();
        String test2 = test2();
        System.out.println(test2);
        while (true) {

        }
    }


    public static void test1() {
        ExecutorService executorService = Executors.newScheduledThreadPool(3);
        log.info("创建线程池");
        for (int i = 0; i < 10; i++) {
            int index = i;
            executorService.execute(() -> {
                log.info("running {}", index);
            });
        }
        executorService.shutdown();
        log.info("关闭线程池");
    }


    public static String test2() {
        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
        log.info("创建线程池");

        executorService.schedule(new Runnable() {
            @Override
            public void run() {
                log.info("start exec --->>>");
            }
        }, 3, TimeUnit.SECONDS);
        return "success";
    }

    public static void test3() {
        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
        log.info("创建线程池");

        executorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                log.info("start exec --->>>");
                try {
                    Thread.sleep(4000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                log.info("end exec --->>>");
            }
        }, 1000, 3000, TimeUnit.MILLISECONDS);
    }

    /**
     * https://blog.csdn.net/weixin_35756522/article/details/81707276
     * @throws Exception
     */
    public static void test4() throws Exception {
        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
        log.info("创建线程池");

        executorService.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                try {
                    log.info("start exec --->>>");
                    Thread.sleep(4000);
                    log.info("end exec --->>>");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }, 1000, 3000, TimeUnit.MILLISECONDS);
    }


    public static void test5() throws Exception {
        log.info("创建timer对象");
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                log.info("start exec --->>>");
            }
        }, 1000, 3000);

    }


    public static void test6() throws Exception {
        log.info("创建timer对象");
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                log.info("start exec --->>>");
            }
        },new Date(),3000);

    }


}
